package com.sanhe.rfmuscle.activity.work;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.menu.app.MyApp;
import com.sanhe.rfmuscle.databinding.ActivityModeBinding;
import com.sanhe.rfmuscle.event.Event;
import com.sanhe.rfmuscle.util.Const;

public class ModeActivity extends BaseWorkActivity {

    ActivityModeBinding binding;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityModeBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        binding.mode1.setOnClickListener(v -> {
            if (checkRent() && checkHandle()) {
                sendTouchEvent((byte) 0, (byte) 4);
            }
            if (Const.jump) {
                Const.mode = 1;
                Intent i = new Intent(ModeActivity.this, WorkActivity.class);
                startActivity(i);
            }
        });
        binding.mode2.setOnClickListener(v -> {
            if (checkRent() && checkHandle()) {
                sendTouchEvent((byte) 0, (byte) 2);
            }
            if (Const.jump) {
                Const.mode = 2;
                Intent i = new Intent(ModeActivity.this, WorkActivity.class);
                startActivity(i);
            }
        });
        binding.mode3.setOnClickListener(v -> {
            if (checkRent() && checkHandle()) {
                sendTouchEvent((byte) 0, (byte) 3);
            }
            if (Const.jump) {
                Const.mode = 3;
                Intent i = new Intent(ModeActivity.this, WorkActivity.class);
                startActivity(i);
            }
        });

        binding.back.setOnClickListener(v -> finish());

        if (Const.warned) {
            Const.warned = false;
            Intent i = new Intent(ModeActivity.this, WarnActivity.class);
            startActivity(i);
        }
    }

    private boolean checkRent() {
        if (Event.rentEvent.getValue() == 1 && Event.num1Event.getValue() == 0) {
            showPointAlert(this);
            return false;
        }
        return true;
    }

    private boolean checkHandle() {
        if (Event.leftBEvent.getValue() <= 0 && Event.rightBEvent.getValue() <= 0) {
            Toast.makeText(this, "No handle connecting", Toast.LENGTH_SHORT).show();
            return false;
        }
        return true;
    }

    @Override
    protected void onResume() {
        super.onResume();
        MyApp.getApp().setIgnore(false);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApp.getApp().setIgnore(true);
    }
}
